package com.mxf.miaosha.controller;

import com.mxf.miaosha.domain.User;
import com.mxf.miaosha.rabbitmq.MQSender;
import com.mxf.miaosha.redis.RedisService;
import com.mxf.miaosha.redis.UserKey;
import com.mxf.miaosha.result.CodeMsg;
import com.mxf.miaosha.result.Result;
import com.mxf.miaosha.service.UserService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    MQSender mqSender;

    @RequestMapping("/")
    @ResponseBody
    public String index() {
        return "hello world";
    }

    @RequestMapping("/success")
    @ResponseBody
    public Result<String> success() {
        return Result.success("访问成功");
    }

    @RequestMapping("/serverError")
    @ResponseBody
    public Result<String> error() {
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("hello")
    public String thymeleaf(Model model) {
        model.addAttribute("name", "zyn");
        return "hello";
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet(int id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx(){
        return Result.success(userService.tx());
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
        User result = redisService.get(UserKey.getById, ""+1, User.class);
        return Result.success(result);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
        User user = new User();
        user.setId(1);
        user.setName("zyn");
        boolean result = redisService.set(UserKey.getById, "" + 1, user);
        return Result.success(result);
    }

//    @RequestMapping("/mq")
//    @ResponseBody
//    public Result<String> mq() {
//        mqSender.send("i love zyn");
//        return Result.success("i love zyn");
//    }
//
//    @RequestMapping("/mq/topic")
//    @ResponseBody
//    public Result<String> mq_topic() {
//        mqSender.sendTopic("i love zyn");
//        return Result.success("i love zyn");
//    }
//
//    @RequestMapping("/mq/fanout")
//    @ResponseBody
//    public Result<String> mq_fanout() {
//        mqSender.sendFanout("i love zyn");
//        return Result.success("i love zyn");
//    }
//
//    @RequestMapping("/mq/headers")
//    @ResponseBody
//    public Result<String> mq_headers() {
//        mqSender.sendHeaders("i love zyn");
//        return Result.success("i love zyn");
//    }
}
